package com.interesting.client.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.interesting.R;

/**
 * 控件
 * 
 * @author yunhe
 * 
 */
public class RadioWithTitle extends RadioGroup {

	private RadioGroup radio;
	private int status = 0;
	private int good = 0;
	private int bad = 0;
	private int old = 0;
	private RadioButton radio_good, radio_bad;

	private RadioEventListener listener;

	public interface RadioEventListener {
		public void event(int state);
	}

	public RadioWithTitle(Context context, String qvalue) {
		super(context);
		init(qvalue);
	}

	public void setRadioEvent(RadioEventListener lis) {
		this.listener = lis;
	}

	public RadioWithTitle(Context context, AttributeSet attrs) {
		super(context, attrs);

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.widget_group_radiobutton, this);

		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LableEditor);
		init(a.getString(R.styleable.LableEditor_qvalue));
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		radio.clearCheck();
		switch (status) {
		case 1:
			radio_good.setEnabled(false);
			radio_bad.setEnabled(true);
			break;
		case -1:
			radio_good.setEnabled(true);
			radio_bad.setEnabled(false);
			break;
		case 0:
			radio_good.setEnabled(true);
			radio_bad.setEnabled(true);
			break;
		default:
			break;
		}
		this.old = status;
		this.status = status;

	}

	public int getGood() {
		return good;
	}

	public void setGood(int good) {
		this.good = good;
		radio_good.setText(good + "");
	}

	public int getBad() {
		return bad;
	}

	public void setBad(int bad) {
		this.bad = bad;
		radio_bad.setText(bad + "");
	}

	public void setTopCheck(Boolean isTop) {
		radio_good.setChecked(isTop);
	}

	public void setDownCheck(Boolean isDown) {
		radio_bad.setChecked(isDown);
	}

	private void init(String LableEditor_qvalue) {
		radio = (RadioGroup) findViewById(R.id.widget_group_radio_button);
		radio_good = (RadioButton) findViewById(R.id.widget_first_radion);
		radio_bad = (RadioButton) findViewById(R.id.widget_second_radion);
		radio_good.setText(good + "");
		radio_bad.setText(bad + "");

		radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup rg, int which) {
				// 获取变更后的选中项的ID
				Log.d("TAG", "原始状态:" + status);
				switch (which) {
				case R.id.widget_first_radion:
					status = 1;
					break;
				case R.id.widget_second_radion:
					status = -1;
					break;

				default:
					status = 0;
					break;
				}
				//Log.d("TAG", "更新状态:" + status);

				radio_good.setText(good + "");
				radio_bad.setText(bad + "");
			}
		});

		radio_good.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 手动点击事件

				if (listener != null) {
					listener.event(status);
					radio_good.setEnabled(false);
					radio_bad.setEnabled(true);
					bad++;
					good++;
					if (old == 0) {
						bad--;
						old=status;
					}
				}
				radio_good.setText(good + "");
				radio_bad.setText(bad + "");
			}
		});
		radio_bad.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 手动点击事件
				if (listener != null) {
					listener.event(status);
					radio_good.setEnabled(true);
					radio_bad.setEnabled(false);
					good--;
					bad--;
					if (old == 0) {
						good++;
						old=status;
					}
				}

				radio_good.setText(good + "");
				radio_bad.setText(bad + "");

			}
		});
	}

}